﻿namespace Dcms.Common
{
    using System;
    using System.Collections;
    using System.Data;
    using System.IO;
    using System.Xml;

    public class DcmsXmlHelper : IDisposable
    {
        private bool _alreadyDispose = false;
        private XmlDocument xmlDoc = new XmlDocument();
        private XmlElement xmlElem;
        private XmlNode xmlNode;

        public static void AddAttribute(string xmlPath, string NodePath, string NodeAttribute1, string NodeAttributeText)
        {
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.Load(xmlPath);
            XmlAttribute nodeAttribute = objXmlDoc.CreateAttribute(NodeAttribute1);
            XmlNode nodePath = objXmlDoc.SelectSingleNode(NodePath);
            nodePath.Attributes.Append(nodeAttribute);
            ((XmlElement) nodePath).SetAttribute(NodeAttribute1, NodeAttributeText);
            objXmlDoc.Save(xmlPath);
        }

        public static void AddParentNode(string xmlPath, string parentNode)
        {
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(xmlPath);
            XmlElement Node = xdoc.CreateElement(parentNode);
            xdoc.DocumentElement.PrependChild(Node);
            xdoc.Save(xmlPath);
        }

        public void CreateXmlRoot(string root)
        {
            this.xmlNode = this.xmlDoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            this.xmlDoc.AppendChild(this.xmlNode);
            this.xmlElem = this.xmlDoc.CreateElement("", root, "");
            this.xmlDoc.AppendChild(this.xmlElem);
        }

        public void CreatXmlNode(string mainNode, string node)
        {
            XmlNode MainNode = this.xmlDoc.SelectSingleNode(mainNode);
            XmlElement objElem = this.xmlDoc.CreateElement(node);
            MainNode.AppendChild(objElem);
        }

        public void CreatXmlNode(string mainNode, string node, string content)
        {
            XmlNode MainNode = this.xmlDoc.SelectSingleNode(mainNode);
            XmlElement objElem = this.xmlDoc.CreateElement(node);
            objElem.InnerText = content;
            MainNode.AppendChild(objElem);
        }

        public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue)
        {
            XmlNode mainNode = this.xmlDoc.SelectSingleNode(MainNode);
            XmlElement objElem = this.xmlDoc.CreateElement(Node);
            objElem.SetAttribute(Attrib, AttribValue);
            mainNode.AppendChild(objElem);
        }

        public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue, string Content)
        {
            XmlNode mainNode = this.xmlDoc.SelectSingleNode(MainNode);
            XmlElement objElem = this.xmlDoc.CreateElement(Node);
            objElem.SetAttribute(Attrib, AttribValue);
            objElem.InnerText = Content;
            mainNode.AppendChild(objElem);
        }

        public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue, string Attrib2, string AttribValue2)
        {
            XmlNode mainNode = this.xmlDoc.SelectSingleNode(MainNode);
            XmlElement objElem = this.xmlDoc.CreateElement(Node);
            objElem.SetAttribute(Attrib, AttribValue);
            objElem.SetAttribute(Attrib2, AttribValue2);
            mainNode.AppendChild(objElem);
        }

        public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue, string Attrib2, string AttribValue2, string Content)
        {
            XmlNode mainNode = this.xmlDoc.SelectSingleNode(MainNode);
            XmlElement objElem = this.xmlDoc.CreateElement(Node);
            objElem.SetAttribute(Attrib, AttribValue);
            objElem.SetAttribute(Attrib2, AttribValue2);
            objElem.InnerText = Content;
            mainNode.AppendChild(objElem);
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool isDisposing)
        {
            if (!this._alreadyDispose)
            {
                if (isDisposing)
                {
                }
                this._alreadyDispose = true;
            }
        }

        ~DcmsXmlHelper()
        {
            this.Dispose();
        }

        public static ArrayList GetSubElementByAttribute(string XmlPath, string FatherElement, string AttributeName, string AttributeValue, int ArrayLength)
        {
            ArrayList al = new ArrayList();
            XmlDocument docXml = new XmlDocument();
            docXml.Load(XmlPath);
            XmlNodeList xx = docXml.DocumentElement.SelectNodes("//" + FatherElement + "[" + AttributeName + "='" + AttributeValue + "']").Item(0).ChildNodes;
            for (int i = 0; i < ArrayLength & i < xx.Count; i++)
            {
                al.Add(xx.Item(i).InnerText);
            }
            return al;
        }

        public static DataSet GetXml(string XmlPath)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(XmlPath);
            return ds;
        }

        public static DataSet GetXmlData(string xmlPath, string XmlPathNode)
        {
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.Load(xmlPath);
            DataSet ds = new DataSet();
            StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
            ds.ReadXml(read);
            return ds;
        }

        public string GetXmlNodeAttribute(string xmlPath, string strNode, string strAttribute)
        {
            string strReturn = "";
            this.xmlDoc.Load(xmlPath);
            try
            {
                XmlAttributeCollection xmlAttr = this.xmlDoc.SelectSingleNode(strNode).Attributes;
                for (int i = 0; i < xmlAttr.Count; i++)
                {
                    if (xmlAttr.Item(i).Name == strAttribute) strReturn = xmlAttr.Item(i).Value;
                }
            }
            catch (XmlException xmle)
            {
                throw xmle;
            }
            return strReturn;
        }

        public string GetXmlNodeValue(string xmlPath, string strNode)
        {
            string strReturn = string.Empty;
            this.xmlDoc.Load(xmlPath);
            try
            {
                strReturn = this.xmlDoc.SelectSingleNode(strNode).InnerText;
            }
            catch (XmlException xmle)
            {
                Console.WriteLine(xmle.Message);
            }
            return strReturn;
        }

        public static string ReadXmlReturnNode(string XmlPath, string Node)
        {
            XmlDocument docXml = new XmlDocument();
            docXml.Load(XmlPath);
            return docXml.GetElementsByTagName(Node).Item(0).InnerText.ToString();
        }

        public static void XmlAttributeEide(string xmlPath, string NodePath, string NodeAttribute1, string NodeAttributeText)
        {
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.Load(xmlPath);
            ((XmlElement) objXmlDoc.SelectSingleNode(NodePath)).SetAttribute(NodeAttribute1, NodeAttributeText);
            objXmlDoc.Save(xmlPath);
        }

        public static void XmlInsertElement(string xmlPath, string MainNode, string Element, string Content)
        {
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.Load(xmlPath);
            XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
            XmlElement objElement = objXmlDoc.CreateElement(Element);
            objElement.InnerText = Content;
            objNode.AppendChild(objElement);
            objXmlDoc.Save(xmlPath);
        }

        public static void XmlInsertElement(string xmlPath, string MainNode, string Element, string Attrib, string AttribContent, string Content)
        {
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.Load(xmlPath);
            XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
            XmlElement objElement = objXmlDoc.CreateElement(Element);
            objElement.SetAttribute(Attrib, AttribContent);
            objElement.InnerText = Content;
            objNode.AppendChild(objElement);
            objXmlDoc.Save(xmlPath);
        }

        public static void XmlInsertNode(string xmlPath, string MailNode, string ChildNode, string Element, string Content)
        {
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.Load(xmlPath);
            XmlNode objRootNode = objXmlDoc.SelectSingleNode(MailNode);
            XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
            objRootNode.AppendChild(objChildNode);
            XmlElement objElement = objXmlDoc.CreateElement(Element);
            objElement.InnerText = Content;
            objChildNode.AppendChild(objElement);
            objXmlDoc.Save(xmlPath);
        }

        public static void xmlnNodeAttributeDel(string xmlPath, string NodePath, string NodeAttribute)
        {
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.Load(xmlPath);
            ((XmlElement) objXmlDoc.SelectSingleNode(NodePath)).RemoveAttribute(NodeAttribute);
            objXmlDoc.Save(xmlPath);
        }

        public static void XmlNodeDelete(string xmlPath, string Node)
        {
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.Load(xmlPath);
            string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
            objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
            objXmlDoc.Save(xmlPath);
        }

        public static void XmlNodeReplace(string xmlPath, string Node, string Content)
        {
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.Load(xmlPath);
            objXmlDoc.SelectSingleNode(Node).InnerText = Content;
            objXmlDoc.Save(xmlPath);
        }

        public void XmlSave(string path)
        {
            this.xmlDoc.Save(path);
        }
    }
}

